/**
 * 
 */
package name.yzhu.converter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yhzhu
 *
 */
public class HtmlNode {
	private boolean leaf=false;
	private List<HtmlNode>nodeList=new ArrayList<HtmlNode>();
	private HtmlNode parent=null;
	private float count = 0.0f;
	private float nodeValue = 0.0f;
	private String name=null;
	private String value=null;
	
	public HtmlNode(String name, String value) {
		super();
		this.name = name;
		this.value = value;
	}
	
	public void addNode(HtmlNode node) {
		nodeList.add(node);
		node.setParent(this);
		
	}

	public void countAdd(float score)	{
		this.count+=score*this.nodeValue;
		if(this.getParent()!=null)
			this.getParent().countAdd(score*this.nodeValue);
	}
	
	public HtmlNode getHighestNode()	{
		HtmlNode max=this;
		for(HtmlNode ivn:this.nodeList)		{
			
			if(ivn.getCount()>max.getCount())
				max=ivn;
			if(!ivn.isLeaf()){
				HtmlNode in=ivn.getHighestNode();				
				if(max.getCount()<in.getCount())
					max=in;
			}
		}
		return max;
	}
	

	
	public void processEvaluate()	{
		for(HtmlNode ivn:this.nodeList)	{
			ivn.evaluate();
			if(!ivn.isLeaf())				
				ivn.processEvaluate();
		}
	}

	public void evaluate() {
		if(!this.isLeaf())	{
			if (getName().equalsIgnoreCase("a"))
				this.nodeValue = -1.5f;
			else if (getName().equalsIgnoreCase("p") ||
					getName().equalsIgnoreCase("h1") ||
					getName().equalsIgnoreCase("h2") ||
					getName().equalsIgnoreCase("h3") ||
					getName().equalsIgnoreCase("h4") ||
					getName().equalsIgnoreCase("h5") )
				
				this.nodeValue = 3;
			else if (getName().equalsIgnoreCase("br"))
				this.nodeValue = 1.5f;
			else if (getName().equalsIgnoreCase("li"))
				this.nodeValue = 1;
			else if (getName().equalsIgnoreCase("div"))
				this.nodeValue = 0.98f;
			else
				this.nodeValue = 1;
		}
		else {
			this.nodeValue = 1;
			this.count=this.value.length()*this.nodeValue;
			this.getParent().countAdd(this.count);
		}
		
	}
	
	public HtmlNode removeGabageTail()	{
		if(this.nodeList.size()>0)		{
			int size=this.nodeList.size();
			for(int i=size-1;i>=0;i--)			{
				HtmlNode in=this.nodeList.get(i);
				if(in.getCount()<0)
					this.nodeList.remove(in);
				else
					break;
			}
		}
		return this;
	}
	
	public void getPlainString(StringBuffer bs)	{
		for(HtmlNode ivn:this.nodeList)	{
			if(ivn.isLeaf())
				bs.append(ivn.getValue());
			else{
				ivn.getPlainString(bs);
				if(ivn.getName().equalsIgnoreCase("p")){
					bs.append("\n\n");
				}
				else if(ivn.getName().equalsIgnoreCase("br") ||
						ivn.getName().equalsIgnoreCase("tr") ||
						ivn.getName().equalsIgnoreCase("table") ||
						ivn.getName().equalsIgnoreCase("tbody")){
					bs.append("\n");
				}
			}
		}
	}
	
	public void printAll(String empty)	{		
		for(HtmlNode ivn:this.nodeList)	{
			if(ivn.isLeaf())
				System.out.println(empty+ivn.getValue());
			else
				ivn.printAll(empty+"  "+ivn.getName()+":"+ivn.getCount()+"  ");
		}
	}

	public List<HtmlNode> findBrotherIncludeSelf() {
		float limit=(float) (this.getCount()*0.3);
		List<HtmlNode> list=new ArrayList<HtmlNode>();
		if(this.getParent()==null || this.getParent().nodeList==null){
			list.add(this);
		} else{
			for(HtmlNode dn:this.getParent().nodeList){
				if(limit<dn.getCount()){
					list.add(dn);
				}
			}
		}
		return list;
	}
	
	public boolean isLeaf() {
		return leaf;
	}

	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	public HtmlNode getParent() {
		return parent;
	}

	public void setParent(HtmlNode parent) {
		this.parent = parent;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public float getCount() {
		return count;
	}
}
